﻿<%@ WebHandler Language="C#" Class="ThirdPartyPersonEdit" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule;

public class ThirdPartyPersonEdit : PageHandlerBase{
    private decimal? id = null;
    private Person _person;
    private PersonManager manager = new PersonManager();
    private Boolean ReadOnly;
    
    
    public  override void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "text/plain";
        if (!string.IsNullOrEmpty(context.Request["id"]))
        {

            try
            {
                id = decimal.Parse(context.Request["id"]);
            }
            catch { }

        }
        if (context.Request.Form.Count > 0)
        {
            Save(context);
        }

    }


    private void Save(HttpContext context)
    {
        string year = null, month = null, day = null;
        //身份证号码是否正确
        if (!string.IsNullOrEmpty(context.Request.Form["txtIdentity"]))
        {

            if (context.Request.Form["txtIdentity"].Length == 18)
            {
                //截取年份
                year = context.Request.Form["txtIdentity"].Substring(6, 4);
                //截取月份
                month = context.Request.Form["txtIdentity"].Substring(10, 2);
                //截取日
                day = context.Request.Form["txtIdentity"].Substring(12, 2);
            }
            else
            {
                //截取年份
                year = "19" + context.Request.Form["txtIdentity"].Substring(6, 2);
                //截取月份
                month = context.Request.Form["txtIdentity"].Substring(8, 2);
                //截取日
                day = context.Request.Form["txtIdentity"].Substring(10, 2);
            }

            try
            {
                new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
            }
            catch
            {
                context.Response.Write("身份证号码错误");
                return;
            }
        }


        System.Text.StringBuilder logcontent = new System.Text.StringBuilder();
        //是否新增
        bool isAdd = string.IsNullOrEmpty(context.Request["id"]);

        ////判断员工编号是否重复
        if (isAdd)
        {
            bool exist = manager.ExistCode(MyStateManager.DomainID, context.Request.Form["txtCode"], false);
            if (exist)
            {
                context.Response.Write("员工编号被使用，不能被再次使用");
                return;
            }
        }


        if (isAdd)
        {
            _person = new Person();
            _person.DOMAINID = MyStateManager.DomainID;
            _person.Code = context.Request.Form["txtCode"];
            _person.Type = 2;
            _person.OrgID = int.Parse(context.Request.QueryString.ToString());
        }
        else
        {
            _person = manager.Load(id.Value);

            logcontent.Append("<b>修改前条目信息</b><br /> ");
            logcontent.Append(" 姓名:" + _person.Name);
            logcontent.Append(" 编号:" + _person.Code);
            logcontent.Append(" 电话:" + _person.Telephone);
            logcontent.Append(" 传真:" + _person.FAX);
            logcontent.Append(" 电子邮箱:" + _person.EMAIL);
            logcontent.Append(" 身份证号:" + _person.Identity);
            logcontent.Append(" 电子邮箱:" + _person.EMAIL);
            logcontent.Append(" 状态 " + (_person.ACTIVE ? "有效" : "失效"));
            logcontent.Append(" 原更新人:" + _person.LAST_UPDATER);
        }
        _person.Name = context.Request.Form["txtName"];
        //更新User 这张表里面的ORG
        if (id != null)
        {
            var userManager = new UserManager();
            var user = userManager.GetUserByPersonID(id.Value);
            if (user != null)
            {
                user.ORGID = _person.OrgID;
                userManager.Save(user);
            }
        }
        _person.Telephone = context.Request.Form["txtTelephone"];
        _person.FAX = context.Request.Form["txtFax"];
        _person.EMAIL = context.Request.Form["txtMail"];
        _person.Sex = context.Request.Form["ddlSex"];
        if (context.Request.Form["ddlPosition"].Length>0)
            _person.Position = context.Request.Form["ddlPosition"];
        _person.Identity = context.Request.Form["txtIdentity"];
        _person.Type = 2;
        _person.ACTIVE = context.Request.Form["chkValid"].Equals("on");
        _person.LAST_UPDATER = MyStateManager.Name;
        if (context.Request.Form["ddlManager"]!="")
        {
            _person.MANAGERID = decimal.Parse(context.Request.Form["ddlManager"]);
        }
        try
        {
            manager.Save(_person);

        }
        catch (Exception ex)
        {

            context.Response.Write("操作失败，错误已经记录日志");
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "人员维护 - 异常:" + ex.Message);
            return;
        }

        if (isAdd)
        {
            logcontent.Append("<br /><b>新增条目信息:</b>");

        }
        else
        {
            logcontent.Append("<br />修改后条目信息:<br />");
        }

        logcontent.Append(" 姓名:" + _person.Name);
        logcontent.Append(" 电话:" + _person.Telephone);
        logcontent.Append(" 传真:" + _person.FAX);
        logcontent.Append(" 电子邮箱:" + _person.EMAIL);
        logcontent.Append(" 身份证号:" + _person.Identity);
        logcontent.Append(" 状态 " + (_person.ACTIVE ? "有效" : "失效"));

        if (isAdd)
        {
            
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change,
                MyStateManager.Name, MyStateManager.UserID.ToString(), "新增人员信息:" + _person.Name, logcontent.ToString());
        }
        else
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change,
               MyStateManager.Name, MyStateManager.UserID.ToString(), "字人员信息更改:" + _person.Name, logcontent.ToString());
        }
        //跳转到主界面
       // context.Response.Redirect("ThirdPartyPersonList.aspx?" + _person.OrgID);
        Result result = new Result();
        result.result = "success";
        result.id = _person.OrgID.ToString();
        var js = new System.Web.Script.Serialization.JavaScriptSerializer();
        context.Response.Write(js.Serialize(result));
    }
 
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

public class Result {

    public String result { get; set; }
    public String id { get; set; }
}